import {
    observable,
    action
} from "mobx";
import axios from '../../utils/http'
import * as api from '../../utils/api'
import {
    message
} from "antd";


class addMiaoSha {
    @observable
    myFrom = {
        keyWord: '', //关键字
        huoDongDate: [], //活动时间
        skBuskcount: '', //团购人数
        skIt: '', //秒杀库存
        skPrice: '', //秒杀价格
        skRuler: '', //秒杀规则
        goodsId: 5, //秒杀商品ID
        goodsName: '',
        isUpdate: false //是否添加
    }

    @observable
    tabData = []





    @action
    selTabData() { //查询秒杀表格
        return axios({
            url: api.GET_ALL_TABDATA,
            method: 'get'
        }).then(data => {
            data.data.code === 200 ? this.tabData = data.data.data : message.warning('系统开小差')
            this.tabData = data.data.data
            return data.data.data
        })
    }


    @action //双向绑定数据
    insertStart = (data, setData) => {
        if (data === 'keyWord') this.myFrom.keyWord = setData.target.value //修改关键字
        else if (data === 'goodsName') this.myFrom.goodsName = setData //修改name
        else if (data === 'huoDongDate') this.myFrom.huoDongDate = setData //修改日期
        else if (data === 'skBuskcount') this.myFrom.skBuskcount = setData.target.value //修改团购人数
        else if (data === 'skPrice') this.myFrom.skPrice = setData.target.value //修改秒杀价格
        else if (data === 'skIt') this.myFrom.skIt = setData.target.value //修改秒杀库存
        else if (data === 'skRuler') this.myFrom.skRuler = setData.target.value //修改秒杀规则
        else if (data === 'goodsId') this.myFrom.goodsId = setData //修改商品ID
        else if (data === 'update') this.myFrom = {
            keyWord: '',
            goodsId: setData.goodsId, //ID
            goodsName:setData.goodsName,//name
            skBuskcount: setData.skBuskcount, //团购人数
            skIt: setData.skIt, //秒杀库存
            skPrice: setData.skPrice, //秒杀价格
            skRuler: setData.skRuler, //秒杀规则
            huoDongDate: setData.huoDongDate, //日期
            skId:setData.skId,
            isUpdate: true
        }
        else if (data === "insert") this.myFrom = {
            keyWord: '', //关键字
            huoDongDate: [], //活动时间
            skBuskcount: '', //团购人数
            skIt: '', //秒杀库存
            skPrice: '', //秒杀价格
            skRuler: '', //秒杀规则
            goodsId: 5, //秒杀商品ID
            goodsName: '',
            isUpdate: false //是否添加
        }
    }

    @observable
    goodsList = []

    @action //查询所有商品
    updateGoods() {
        axios({
            url: api.GET_ALLGOODS,
            method: 'get',
        }).then(data => {
            this.goodsList = data.data.data
        })
    }

    @action //新增一条限时秒杀
    insertData() {
        let skAst = new Date(this.myFrom.huoDongDate[0]).toLocaleString()
        let skAed = new Date(this.myFrom.huoDongDate[1]).toLocaleString()
        if(this.myFrom.goodsId,this.myFrom.skBuskcount,this.myFrom.skIt,this.myFrom.skPrice,this.myFrom.skRuler,this.myFrom.huoDongDate){
            if (this.myFrom.isUpdate && this.myFrom.skId) { //判断是否修改
                console.log(this.myFrom.skId,this.myFrom.goodsId)
            return axios({//修改
                url: api.UPRDATE_TAB,
                method: 'post',
                params: {
                    skId:this.myFrom.skId,
                    goodsId:this.myFrom.goodsId ,
                    skBuskcount: this.myFrom.skBuskcount,
                    skIt: this.myFrom.skIt,
                    skPrice: this.myFrom.skPrice,
                    skRuler: this.myFrom.skRuler,
                    skAst,
                    skAed
                }
            }).then(data=>{
                console.log(data)
                message.success('修改成功')
                return 200
            })
        } else if(!this.myFrom.isUpdate) {//新增
            return axios({
                url: api.INSERT_MIAOSHA,
                method: 'post',
                data: {
                    goodsId: this.myFrom.goodsId,
                    skBuskcount: this.myFrom.skBuskcount,
                    skIt: this.myFrom.skIt,
                    skPrice: this.myFrom.skPrice,
                    skRuler: this.myFrom.skRuler,
                    skAst,
                    skAed
                }
            }).then(data => {
                if (data.data.code === 200) {
                    message.success('添加成功')
                    return 200
                } else {
                    return 500
                }

            })
        }else{
            message.warning('请先完善基础信息')
        }
        }else{
            message.warning('请先完善基础信息')
        }
        

    }

    @action //修改一条
    updateData(data) {
        this.updateData(data)
        // return project
    }

    @action //删除数据
    delTabData(id) {
        return axios({
            url: api.DEL_TABDATA,
            method: 'get',
            params: {
                skId: id
            }
        }).then(data => {
            console.log(data)
            if (data.data.code === 200) {
                return data.data.data
            }
        })
    }
}

export default addMiaoSha